package com.it.myd.controller.main.module;

import com.it.myd.bean.box.MsgBoard;
import com.it.myd.bean.box.Record;
import com.it.myd.bean.main.ReturnValue;
import com.it.myd.bean.main.User;
import com.it.myd.service.main.date.RecordService;
import com.it.myd.service.main.module.msgboard.MsgBoardService;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/module/msgbd")
public class MsgBoardController {

    @Autowired
    private MsgBoardService msgBoardService;

    @Autowired
    private RecordService recService;

    @RequestMapping("addMsgBoard")
    @ResponseBody
    public ReturnValue addMsgBoard(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String msg_content = request.getParameter("msg_content");
        String msg_parid = request.getParameter("msg_parid");
        MsgBoard msgBoard = new MsgBoard();
        msgBoard.setUser(user);
        msgBoard.setMsg_content(msg_content);
        if(msg_parid!=null && !msg_parid.equals("")){
            msgBoard.setMsg_parid(Integer.parseInt(msg_parid));
            msgBoard.setMsg_istop(0);
        }else{
            msgBoard.setMsg_istop(1);
        }
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        msgBoard.setMsg_time(sdf.format(System.currentTimeMillis()));
        msgBoard.setMsg_type(61000);

        msgBoardService.addMsgBoard(msgBoard);

        ServletContext application = request.getServletContext();
        User u = (User) session.getAttribute("user");
        MsgBoard mb = new MsgBoard();
        mb.setUser(u);
        mb.setMsg_type(61000);
        List<MsgBoard> msgBoards = msgBoardService.GetMsgBoards(mb);
        application.setAttribute("msgBoards",msgBoards);

        ReturnValue returnValue = new ReturnValue();
        returnValue.isSuccess("发送成功");
        return returnValue;
    }

    @RequestMapping("addRecord")
    @ResponseBody
    public ReturnValue addRecord(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String rec_type = request.getParameter("rec_type");
        String obj_id = request.getParameter("obj_id");
        String passive_id = request.getParameter("passive_id");
        Record rec = new Record();
        rec.setRec_type(rec_type);
        rec.setObj_id(Integer.parseInt(obj_id));
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        rec.setRecordtime(sdf.format(System.currentTimeMillis()));
        rec.setActiveman(String.valueOf(user.getUser_id()));
        rec.setPassiveman(passive_id);
        recService.addRecord(rec);
        MsgBoard msgBoard = new MsgBoard();
        msgBoard.setMsg_id(Integer.parseInt(obj_id));
        if(rec_type.equals("lybupcount")){
            msgBoardService.addUpCount(msgBoard);
        }else{
            msgBoardService.addDownCount(msgBoard);
        }
        ServletContext application = request.getServletContext();
        User u = (User) session.getAttribute("user");
        MsgBoard mb = new MsgBoard();
        mb.setUser(u);
        mb.setMsg_type(61000);
        List<MsgBoard> msgBoards = msgBoardService.GetMsgBoards(mb);
        application.setAttribute("msgBoards",msgBoards);
        ReturnValue returnValue = new ReturnValue();
        returnValue.isSuccess();
        return returnValue;
    }

    @RequestMapping("unEffect")
    @ResponseBody
    public ReturnValue unEffect(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String rec_type = request.getParameter("rec_type");
        String obj_id = request.getParameter("obj_id");
        Record rec = new Record();
        rec.setObj_id(Integer.parseInt(obj_id));
        rec.setActiveman(String.valueOf(user.getUser_id()));
        recService.unEffect(rec);
        MsgBoard msgBoard = new MsgBoard();
        msgBoard.setMsg_id(Integer.parseInt(obj_id));
        if(rec_type.equals("lybupcount")){
            msgBoardService.subUpCount(msgBoard);
        }else{
            msgBoardService.subDownCount(msgBoard);
        }
        ServletContext application = request.getServletContext();
        User u = (User) session.getAttribute("user");
        MsgBoard mb = new MsgBoard();
        mb.setUser(u);
        mb.setMsg_type(61000);
        List<MsgBoard> msgBoards = msgBoardService.GetMsgBoards(mb);
        application.setAttribute("msgBoards",msgBoards);
        ReturnValue returnValue = new ReturnValue();
        returnValue.isSuccess();
        return returnValue;
    }

}
